const Router = require('koa-router')

const router = new Router({ prefix: '/users' })

const auth = require('../middleware/auth')
const { checkUser } = require('../middleware/check')
const {
  checkUserExist,
  checkTopicExist,
  checkAnswerExist
} = require('../middleware/exist')

const {
  find,
  findById,
  create,
  update,
  delete: del,
  login,
  listFollowing,
  follow,
  unfollow,
  listFollowers,
  followTopic,
  unfollowTopic,
  listFollowTopic,
  listQuestions,
  likeAnswer,
  unlikeAnswer,
  listLikingAnswers,
  dislikeAnswer,
  unDislikeAnswer,
  listDisLikingAnswers,
  collectingAnswer,
  unCollectingAnswer,
  listCollectingAnswers
} = require('../controllers/users')

// 登录
router.post('/login', login)

// 获取（整体）
router.get('/', find)

// 新增
router.post('/', create)

// 获取（单一）
router.get('/:id', findById)

// 更新
router.patch('/:id', auth, checkUser, update)

//删除
router.delete('/:id', auth, checkUser, del)

// 关注
router.put('/following/:id', auth, checkUserExist, follow)

// 取消关注
router.delete('/unfollowing/:id', auth, checkUserExist, unfollow)

// 某人关注的人
router.get('/:id/following', listFollowing)

// 关注某人的人
router.get('/:id/followers', listFollowers)

// 关注话题
router.put('/following_topic/:id', auth, checkTopicExist, followTopic)

// 取消关注话题
router.delete('/unfollowing_topic/:id', auth, checkTopicExist, unfollowTopic)

// 某人关注的话题
router.get('/:id/follow_topics', listFollowTopic)

// 获取某人提出的问题
router.get('/:id/questions', listQuestions)

// 获取某人赞过的答案
router.get('/:id/liking_answers', listLikingAnswers)

// 某人赞一下答案
router.put(
  '/like_answer/:id',
  auth,
  checkAnswerExist,
  // NOTE: 互斥 赞过的答案取消踩
  likeAnswer,
  unDislikeAnswer
)

// 某人取消赞过的答案
router.delete('/unlike_answer/:id', auth, checkAnswerExist, unlikeAnswer)

// 获取某人踩过的答案
router.get('/:id/disliking_answers', listDisLikingAnswers)

// 某人踩一下答案
router.put(
  '/dislike_answer/:id',
  auth,
  checkAnswerExist,
  // NOTE: 互斥 踩过的答案取消赞
  dislikeAnswer,
  unlikeAnswer
)

// 某人取消踩答案
router.delete('/undislike_answer/:id', auth, checkAnswerExist, unDislikeAnswer)

// 获取某人收藏过的答案
router.get('/:id/collecting_answers', listCollectingAnswers)

// 某人收藏答案
router.put('/collect_answer/:id', auth, checkAnswerExist, collectingAnswer)

// 某人取消收藏答案
router.delete(
  '/uncollec_answer/:id',
  auth,
  checkAnswerExist,
  unCollectingAnswer
)

module.exports = router
